//
//  YHMeSetUserInfoViewController.m
//  YHApp
//
//  Created by -- on 2021/6/19.
//

#import "HAUserInfoVC.h"
#import "HAUserInfoModel.h"
#import "HAChooseTabPopView.h"
#import "HAAddressPopView.h"
#import "YWAlertView.h"

@interface HAUserInfoVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HAUserInfoModel *model;

/***/
@property (nonatomic, strong) UITextField *mobileTextField;
/***/
@property (nonatomic, strong) UITextField *nameTextField;
/***/
@property (nonatomic, strong) UITextField *identityTextField;
/***/
@property (nonatomic, strong) UITextField *bondTextField;
/***/
@property (nonatomic, strong) UITextField *addressTextField;

/***/
@property (nonatomic, strong) NSString *name;
/***/
@property (nonatomic, strong) NSString *identity;
/***/
@property (nonatomic, strong) NSString *region;
/***/
@property (nonatomic, strong) NSString *address;
/***/
@property (nonatomic, strong) NSString *type;

/***/
@property (nonatomic, assign) NSInteger selAddressType;

/***/
@property (nonatomic, strong) UITextField *companyNameTextField;
/***/
@property (nonatomic, strong) UITextField *companyBoosNameTextField;
/***/
@property (nonatomic, strong) UITextField *companyCodeTextField;
/***/
@property (nonatomic, strong) UITextField *companyMoneyTextField;
/***/
@property (nonatomic, strong) UITextField *companyAddressField;

/***/
@property (nonatomic, strong) NSString *company_name;
/***/
@property (nonatomic, strong) NSString *company_legal_person;
/***/
@property (nonatomic, strong) NSString *company_business;
/***/
@property (nonatomic, strong) NSString *company_capital;
/***/
@property (nonatomic, strong) NSString *company_address;
/***/
@property (nonatomic, strong) NSString *company_open_date;
/***/
@property (nonatomic, strong) NSString *company_region;

/***/
@property (nonatomic, assign) BOOL isCanEditName;


@property (nonatomic,strong) id <YWAlertViewProtocol>dateAlert;

@end

@implementation HAUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];

    [self loadData];


    [self.tableView reloadData];
    
    // Do any additional setup after loading the view.
}

- (void)loadData {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"account/info";
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                self.model = [HAUserInfoModel mj_objectWithKeyValues:dataModel.data];
                self.name = self.model.name;
                if ([NSString isEmptyString:self.model.name]) {
                    self.isCanEditName = YES;
                }else {
                    self.isCanEditName = NO;
                }
                self.identity = self.model.identity;
                self.region = self.model.region;
                self.address = self.model.address;
                if (self.model.is_public==1) {
                    self.type = @"1";
                }else {
                    self.type = @"0";
                }
                self.company_name = self.model.company.name;
                self.company_legal_person = self.model.company.legal_person;
                self.company_business = self.model.company.business;
                self.company_capital = self.model.company.capital;
                self.company_address = self.model.company.address;
                self.company_open_date = self.model.company.open_date;
                self.company_region = self.model.company.region;

                [self.dataArray removeAllObjects];
                [self.dataArray addObject:@"手机号"];
                [self.dataArray addObject:@"代理名称"];
                [self.dataArray addObject:@"身份证号"];
                [self.dataArray addObject:@"保证金"];
                [self.dataArray addObject:@"省市区/县1"];
                [self.dataArray addObject:@"街道地址"];
                [self.dataArray addObject:@"客户类型"];
                if (self.model.is_public == 1) {
                    [self.dataArray addObject:@"企业信息"];
                    [self.dataArray addObject:@"企业名称"];
                    [self.dataArray addObject:@"法人姓名"];
                    [self.dataArray addObject:@"机构代码"];
                    [self.dataArray addObject:@"注册资本"];
                    [self.dataArray addObject:@"注册时间"];
                    [self.dataArray addObject:@"省市区/县2"];
                    [self.dataArray addObject:@"公司地址"];
                }
                [self.dataArray addObject:@"提交"];
                [self.tableView reloadData];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

-(void)mitButtonClick {
    [self.view endEditing:YES];
    
    NSDictionary *dict = @{
        @"name":self.name,
        @"identity":self.identity,
        @"region":self.region,
        @"address":self.address,
        @"type":self.type,
        @"company":@{
                @"name":self.company_name?:@"",
                @"legal_person":self.company_legal_person?:@"",
                @"business":self.company_business?:@"",
                @"company_capital":self.company_capital?:@"",
                @"address":self.company_address?:@"",
        },
        @"company_open_date":self.company_open_date?:@"",
        @"company_region":self.company_region?:@"",
    };
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodPOST;
    request.requestUrl = @"account/profile";
    request.parms = dict;
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"提交成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

- (void)initUI{
    self.baseTLabel.text = @"个人信息";

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"企业信息"]) {
        return 64;
    }else if ([title isEqualToString:@"提交"]) {
        return 54+60;
    }
    return 54;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if ([title isEqualToString:@"手机号"]) {
        [self setCellTextContent:cell.contentView title:title content:self.model.mobile isLine:YES];
    }
    if ([title isEqualToString:@"代理名称"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.name isLine:YES ];
    }
    if ([title isEqualToString:@"身份证号"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.identity isLine:YES];
    }
    if ([title isEqualToString:@"保证金"]) {
        [self setCellTextContent:cell.contentView title:title content:[NSString stringWithFormat:@"%@(万元)",self.model.deposit] isLine:YES];
    }
    if ([title isEqualToString:@"省市区/县1"]) {
        [self setCellTextContent:cell.contentView title:@"省市区/县" content:self.region isLine:YES];
    }
    if ([title isEqualToString:@"街道地址"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.address isLine:YES];
    }
    if ([title isEqualToString:@"客户类型"]) {
        NSString *context =  @"";
        if ([self.type isEqualToString:@"1"]) {
            context = @"公司";
        }else {
            context = @"个人";
        }
        [self setCellTextContent:cell.contentView title:title content:context isLine:YES];
    }
    if ([title isEqualToString:@"企业信息"]) {
        [self setCellTextContent:cell.contentView title:title content:@"" isLine:NO];
    }else if ([title isEqualToString:@"企业名称"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.company_name isLine:YES];
    }else if ([title isEqualToString:@"法人姓名"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.company_legal_person isLine:YES];
    }else if ([title isEqualToString:@"机构代码"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.company_business isLine:YES];
    }else if ([title isEqualToString:@"注册资本"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.company_capital isLine:YES];
    }else if ([title isEqualToString:@"注册时间"]) {
        [self setCellTextContent:cell.contentView title:title content:self.company_open_date?:@"注册时间" isLine:YES];
    }else if ([title isEqualToString:@"省市区/县2"]) {
        [self setCellTextContent:cell.contentView title:title content:@"省市区/县" isLine:YES];
    }else if ([title isEqualToString:@"公司地址"]) {
        [self setCellFieldContent:cell.contentView title:title content:self.company_address isLine:YES];
    }else if ([title isEqualToString:@"提交"]) {
        [self setCellMit:cell.contentView];
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    WS(weakSelf)
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"省市区/县1"]) {
        self.selAddressType = 1;
        [self laodAddress];
    }else if ([title isEqualToString:@"客户类型"]) {
        NSMutableArray *dicts = [NSMutableArray array];
        [dicts addObject:@{@"color":@"#373737",@"title":@"个人"}];
        [dicts addObject:@{@"color":@"#373737",@"title":@"公司"}];
        HAChooseTabPopView *menu = [[HAChooseTabPopView alloc]initWithDicts:dicts title:@"客户类型"];
        [menu show];
        menu.YHChooseTabMenuCellBlock = ^(NSString * _Nonnull title) {
            
            [weakSelf.dataArray removeAllObjects];
            [weakSelf.dataArray addObject:@"手机号"];
            [weakSelf.dataArray addObject:@"代理名称"];
            [weakSelf.dataArray addObject:@"身份证号"];
            [weakSelf.dataArray addObject:@"保证金"];
            [weakSelf.dataArray addObject:@"省市区/县1"];
            [weakSelf.dataArray addObject:@"街道地址"];
            [weakSelf.dataArray addObject:@"客户类型"];

            if ([title isEqualToString:@"公司"]) {
                weakSelf.type = @"1";
                [weakSelf.dataArray addObject:@"企业信息"];
                [weakSelf.dataArray addObject:@"企业名称"];
                [weakSelf.dataArray addObject:@"法人姓名"];
                [weakSelf.dataArray addObject:@"机构代码"];
                [weakSelf.dataArray addObject:@"注册资本"];
                [weakSelf.dataArray addObject:@"注册时间"];
                [weakSelf.dataArray addObject:@"省市区/县2"];
                [weakSelf.dataArray addObject:@"公司地址"];
            }else {
                weakSelf.type = @"0";
            }
            [weakSelf.dataArray addObject:@"提交"];
            [weakSelf.tableView reloadData];

            [weakSelf.tableView reloadData];
        };
    }else if ([title isEqualToString:@"注册时间"]) {
        [self.dateAlert show];
    }else if ([title isEqualToString:@"省市区/县2"]) {
        self.selAddressType = 2;
        [self laodAddress];
    }
}

- (id<YWAlertViewProtocol>)dateAlert{
    if (!_dateAlert) {
        WS(weakSelf)
        _dateAlert = [YWAlertView alertViewWithTitle:@"注册时间" preferredStyle:YWAlertViewStyleDatePicker2 footStyle:YWAlertPublicFootStyleDefalut bodyStyle:YWAlertStyleShowYearMonthDay cancelButtonTitle:@"取消" sureButtonTitles:@"确定" handler:^(NSInteger buttonIndex, id  _Nullable value) {
            if (!value || buttonIndex == 100) {
                return;
            }
            weakSelf.company_open_date = [NSString stringWithFormat:@"%@",value];
            [weakSelf.tableView reloadData];
        }];
    }
    return _dateAlert;
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.nameTextField) {
        self.name = textField.text;
    }
    if (textField == self.identityTextField) {
        self.identity = textField.text;
    }
    if (textField == self.addressTextField) {
        self.address = textField.text;
    }
    if (textField == self.companyNameTextField) {
        self.company_name = textField.text;
    }
    if (textField == self.companyBoosNameTextField) {
        self.company_legal_person = textField.text;
    }
    if (textField == self.companyCodeTextField) {
        self.company_business = textField.text;
    }
    if (textField == self.companyMoneyTextField) {
        self.company_capital = textField.text;
    }
    if (textField == self.companyAddressField) {
        self.company_address = textField.text;
    }
    
}

-(void)setCellFieldContent:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isLine {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#111a34" text:title alignment:0];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(88);
    }];
    
    UITextField *textField = [[UITextField alloc]init];
    textField.delegate = self;
    textField.textColor = [UIColor colorWithHexString:@"#121B32"];
    textField.font = [UIFont systemFontOfSize:16];
    textField.textAlignment = 2;
    [contentView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(titLabel.mas_right);
        make.height.mas_offset(32);
    }];

    if (isLine == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor kLineColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentView).offset(20);
            make.right.equalTo(contentView).offset(-20);
            make.height.equalTo(@0.6);
            make.bottom.equalTo(contentView);
        }];
    }
    
    textField.text = content;

    if ([title isEqualToString:@"代理名称"]) {
        self.nameTextField = textField;
        textField.placeholder = @"请输入真实姓名";
        textField.enabled = self.isCanEditName;
    }
    if ([title isEqualToString:@"身份证号"]) {
        self.identityTextField = textField;
        textField.placeholder = @"请输入身份证号";
    }
    if ([title isEqualToString:@"街道地址"]) {
        self.addressTextField = textField;
        textField.placeholder = @"请输入街道地址";
    }
    if ([title isEqualToString:@"企业名称"]) {
        self.companyNameTextField = textField;
        textField.placeholder = @"企业名称";
    }
    if ([title isEqualToString:@"法人姓名"]) {
        self.companyBoosNameTextField = textField;
        textField.placeholder = @"法人姓名";
    }
    if ([title isEqualToString:@"机构代码"]) {
        self.companyCodeTextField = textField;
        textField.placeholder = @"组织机构代码";
    }
    if ([title isEqualToString:@"注册资本"]) {
        self.companyMoneyTextField = textField;
        textField.placeholder = @"注册资本(万元)";
    }
    if ([title isEqualToString:@"公司地址"]) {
        self.companyAddressField = textField;
        textField.placeholder = @"公司详细地址";
    }

}

-(void)setCellTextContent:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isLine {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }

    UILabel *titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#111a34" text:title alignment:0];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(88);
    }];

    UILabel *conLabel = [UILabel ha_BLabelWithFont:16 textColor:@"#888888" text:content alignment:2];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(titLabel.mas_right);
        make.height.mas_offset(32);
    }];

    if (isLine == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor kLineColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentView).offset(20);
            make.right.equalTo(contentView).offset(-20);
            make.height.equalTo(@0.6);
            make.bottom.equalTo(contentView);
        }];
    }
}


-(void)setCellMit:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UIButton *mitButton = [[UIButton alloc]init];
    [mitButton setTitle:@"提交" forState:UIControlStateNormal];
    [mitButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    mitButton.titleLabel.font = [UIFont systemFontOfSize:20];
    mitButton.backgroundColor = [UIColor colorWithHexString:@"#19191A"];
    [mitButton addTarget:self action:@selector(mitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:mitButton];
    [mitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
        make.height.mas_offset(54);
    }];
}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}


-(void)laodAddress {
    WS(weakSelf)
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"region/lists.html";
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                NSArray *arr = [HAAddressProvinceMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                
                HAAddressPopView *menu = [[HAAddressPopView alloc]initWithDZArray:arr];
                [menu show];
                menu.HAAddressPopViewSureBlock = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull area) {
                    if (weakSelf.selAddressType == 1) {
                        weakSelf.region = [NSString stringWithFormat:@"%@/%@/%@",province,city,area];
                    }else if (weakSelf.selAddressType == 2) {
                        weakSelf.company_region = [NSString stringWithFormat:@"%@/%@/%@",province,city,area];
                    }
                    [weakSelf.tableView reloadData];
                };
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

@end
